package com.micro.frog.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micro.frog.common.contants.Symbol;
import com.micro.frog.rbac.entity.UserDO;
import com.micro.frog.rbac.entity.UserRoleDO;
import com.micro.frog.rbac.mapper.UserMapper;
import com.micro.frog.rbac.mapper.UserRoleMapper;
import com.micro.frog.rbac.service.UserRoleService;
import com.micro.frog.rbac.vo.RoleUserVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author :  ljh
 * @date : 2018/4/12
 **/
@Service
@AllArgsConstructor
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleDO> implements UserRoleService {

    private final UserMapper userMapper;

    @Override
    public List<RoleUserVO> queryUserByRoleId(Long roleId) {
        QueryWrapper<UserRoleDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        queryWrapper.eq("is_deleted", "N");
        List<UserRoleDO> userRoles = this.list(queryWrapper);
        List<RoleUserVO> voList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userRoles)) {
            Set<Long> userIds = userRoles.stream().map(UserRoleDO::getUserId).collect(Collectors.toSet());
            QueryWrapper<UserDO> userWrapper = new QueryWrapper<>();
            userWrapper.in("user_id", userIds);
            userWrapper.eq("is_deleted", "N");
            List<UserDO> userList = userMapper.selectList(userWrapper);
            if (!CollectionUtils.isEmpty(userList)) {
                for (UserDO user : userList) {
                    RoleUserVO userVo = new RoleUserVO();
                    userVo.setUserId(user.getId());
                    userVo.setLoginName(user.getLoginName());
                    userVo.setUserName(user.getUserName());
                    voList.add(userVo);
                }
            }
        }
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void addUserRole(Long roleId, List<Long> userIds) {
        for (Long userId : userIds) {
            if (null != userId) {
                UserRoleDO userRole = new UserRoleDO();
                userRole.setRoleId(roleId);
                userRole.setUserId(userId);
                this.save(userRole);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateRoleUsers(Long roleId, String userIds) {
        this.deleteByRoleId(roleId);
        if (!StringUtils.isEmpty(userIds)) {
            List<UserRoleDO> userRoles = new ArrayList<>();
            for (String userId : userIds.split(Symbol.COMA)) {
                if (null != userId) {
                    UserRoleDO userRole = new UserRoleDO();
                    userRole.setRoleId(roleId);
                    userRole.setUserId(Long.parseLong(userId));
                    userRoles.add(userRole);
                }
            }
            this.saveBatch(userRoles);
        }
    }

    private void deleteByRoleId(Long roleId) {
        UpdateWrapper<UserRoleDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("role_id", roleId);
        this.remove(updateWrapper);
    }

    @Override
    public List<UserRoleDO> queryList(UserRoleDO userRole) {
        QueryWrapper<UserRoleDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", "N");
        queryWrapper.setEntity(userRole);
        return this.list(queryWrapper);
    }

    @Override
    public boolean add(UserRoleDO userRole) {
        return this.save(userRole);
    }

    @Override
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean update(UserRoleDO userRole) {
        return this.updateById(userRole);
    }
}
